package com.flower.wx.handler;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lavender
 * @version 1.0
 * @date 2024-07-19 0:13
 */
@Component
public class WxChatMsgFactory implements InitializingBean {


    @Resource
    private List<WxChatMsgHandler> wxChatMsgHandlers;


    private Map<WxChatMsgTypeEnum, WxChatMsgHandler> map = new HashMap<>();

    public WxChatMsgHandler getWxChatMsgHandler(String wxChatMsgTypeEnum) {
        // 拿到对应的枚举
        WxChatMsgTypeEnum byMsgType = WxChatMsgTypeEnum.getByMsgType(wxChatMsgTypeEnum);

        return map.get(byMsgType);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        wxChatMsgHandlers.forEach(wxChatMsgHandler -> {
            map.put(wxChatMsgHandler.getWxChatMsgTypeEnum(), wxChatMsgHandler);
        });


    }
}
